# Request模块
import json
import os

import requests
from fake_useragent import UserAgent
import parsel


class getInfoByRequest:
    def __init__(self):
        pass

    @staticmethod
    def get_weather_data(url, Referer):
        # 构造请求头
        headers = {
            'User-Agent': UserAgent().firefox,
            'Referer': Referer,  # http://tianqi.2345.com/wea_history/58606.htm
        }
        # 发送请求
        response = requests.get(url=url, headers=headers)
        # 获取响应数据
        if response.status_code != 200:
            print('请求失败')
            return None
        else:
            # 解析响应数据
            json_data = response.json()  # 提取 json 数据
            # 获取 html 数据
            html_data = json_data['data']
            return html_data

    # 爬取某一年度各省的人均收入
    @staticmethod
    def getIncomeData(url):
        # 构造请求头
        headers = {
            'User-Agent': UserAgent().firefox,
        }
        # 发送请求获取数据
        response = requests.get(url=url, headers=headers)
        return response

    @staticmethod
    def getUrl(url):
        print('正在爬取' + url)
        # 获取近30日所有天气预报的 url 列表
        header = {
            'User-Agent': UserAgent().firefox,
        }
        # 发送请求
        response = requests.get(url=url, headers=header)
        if response.status_code != 200:
            print('请求失败')
            return None
        else:
            # 解析相应数据
            html_data = response.content.decode('utf-8')
            # 构造解析对象
            html_data = parsel.Selector(html_data)
            # 根据 CSS 选择器获取数据
            url_list = html_data.css('.result-item .img-box a::attr(href)').getall()
            # print(json.dumps(url_list, ensure_ascii=False, indent=4))
            return url_list

    @staticmethod
    def getWeatherUrl(url):
        url_list = getInfoByRequest().getUrl(url)
        url = url.replace('index', 'index_2')
        url_list += getInfoByRequest().getUrl(url)
        return url_list

    @staticmethod
    def getVideo(url_list):
        # 爬取视频并转存为mp4格式
        if not os.path.exists('video'):
            os.makedirs('video')

        # 构造请求头
        headers = {
            'User-Agent': UserAgent().firefox,
        }

        # 视频链接列表
        video_url_list = []

        # 遍历 url 列表
        for url in url_list:
            # 发送请求
            response = requests.get(url=url, headers=headers)
            # 获取响应数据
            if response.status_code != 200:
                print('请求失败')
                return None
            else:
                # 解析响应数据
                html_data = response.content.decode('utf-8')
                # print(html_data)
                # 构造解析对象
                html_data = parsel.Selector(html_data)
                # 获取视频链接
                video_url = html_data.css('.content-left .player-box video source::attr(src)').getall()
                # print(video_url)
                video_url_list.append(video_url[0])
        print(video_url_list)

        # 爬取视频并转存为mp4格式
        # 发起请求获取视频的二进制数据
        filename_list = []  # 存放所有的文件名 用来对视频进行去重
        for video_url in video_url_list:
            # 发起请求
            response = requests.get(url=video_url, headers=headers)
            video_data = response.content
            filename = video_url.split('/')[4] + '年' + video_url.split('/')[5] + '月' + video_url.split('/')[
                6] + '日' + '天气预报视频' + '.mp4'
            if filename not in filename_list:
                filename_list.append(filename)
                print('正在爬取' + filename)
                with open('video/' + filename, 'wb') as file:
                    file.write(video_data)
            else:
                continue

    @staticmethod
    def getFutureWeather(url):
        # 构造请求头
        headers = {
            'User-Agent': UserAgent().firefox,
        }
        # 发起请求
        response = requests.get(url=url, headers=headers)
        # 获取 json 数据
        json_data = response.json()

        return json_data  # 返回字典类型数据
